<?php
/**
 * Notes:
 * User: wudg <544038230@qq.com>
 * Date: 2025/3/31 16:42
 */

namespace App\Service\User;


use App\Constants\User\UserTypeConst;
use App\Dao\Users\UserCategoryDao;
use App\Dao\Users\UsersCardsInfoDao;
use App\Dao\Users\UsersDao;
use App\Exception\ServiceException;
use Hyperf\Di\Annotation\Inject;

class UserService
{

    #[Inject]
    protected UsersDao $usersDao;

    #[Inject]
    protected UserCategoryDao $userCategoryDao;


    #[Inject]
    protected UsersCardsInfoDao $usersCardsInfoDao;

    public function userInfo()
    {
        $userInfo = auth()->user();

        return $userInfo->toArray();
    }

    /**
     * 判断用户是否为子账号
     * @param $userInfo
     * @return bool
     */
    public static function isSubAccount($userInfo =  null)
    {
        if(empty($userInfo)) $userInfo = auth()->user();

        return $userInfo->user_type == UserTypeConst::SUB_ACCOUNT->value;
    }


    /**
     * 是否为普通会员
     * @param $userInfo
     * @return bool
     */
    public function isMemberAccount($userInfo =  null)
    {
        if(empty($userInfo)) $userInfo = auth()->user();

        return $userInfo->user_type == UserTypeConst::NORMAL_MEMBER->value;
    }


    /**
     * 主账号
     * @param $userInfo
     * @return bool
     */
    public function isMainAccount($userInfo =  null)
    {
        if(empty($userInfo)) $userInfo = auth()->user();

        return $userInfo->user_type == UserTypeConst::MAIN_ACCOUNT->value;
    }


    public function userCateTotals()
    {
        $userInfo = auth()->user();

        $uid = $userInfo->getId();
        if(UserService::isSubAccount($userInfo))
        {
            $uid = $userInfo->soft_id;
        }
        $cateNum = $this->userCategoryDao->getCountCateNum($uid);

        $cardMemberNum = $this->usersCardsInfoDao->getUserCardMembersNums($uid);

        return [$cateNum,$cardMemberNum];
    }

    public function userUpdate(array $params)
    {
        $userInfo = auth()->user();

        if(!empty($params['avatar'])) $userInfo->avatar = $params['avatar'];
        if(!empty($params['nick_name'])) $userInfo->nick_name = $params['nick_name'];
        if(!empty($params['company_name'])) $userInfo->company_name = $params['company_name'];
        if(!empty($params['name'])) $userInfo->name = $params['name'];
        if(!empty($params['birthday'])) $userInfo->birthday = $params['birthday'];
        if(!empty($params['email'])) $userInfo->email = $params['email'];
        if(!empty($params['sex'])) $userInfo->sex = $params['sex'];
        if(!empty($params['remark'])) $userInfo->remark = $params['remark'];
        if(!empty($params['user_type'])) $userInfo->user_type = $params['user_type'];
        if(!empty($params['password'])) $userInfo->password = password_hash(trim($params['password']),PASSWORD_DEFAULT);

        /**
         * 修改手机号自动变更账号信息
         */
        if(!empty($params['phone']))
        {
            if($params['phone'] != $userInfo->phone) {
                $resInfo = $this->usersDao->getInfoByPhone($params['phone']);
                if($resInfo) throw new ServiceException("手机号已存在，请更换！");
                $userInfo->phone = $params['phone'];
            }
            $userInfo->account = $params['phone'];
        }
        $res = $userInfo->save();

        if(!$res) throw new ServiceException("更新失败");
    }
}